package com.hl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.Company;
import com.hl.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    //分页查询
    public Map<String,Object> queryByPage(Integer pageNum, Integer pageSize){
        Page<Object> objects = PageHelper.startPage(pageNum, pageSize);
        List<Company> companies = companyMapper.queryAll();
        PageInfo<Object> objectPageInfo = new PageInfo<>(objects);
        long total = objectPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("companies",companies);
        map.put("total",total);
        return map;

    }


    //动态条件查询
    public  Map<String,Object> querySelected(Company company){
        Page<Object> objects = PageHelper.startPage(1, 10);
        List<Company> companies = companyMapper.querySelected(company);
        PageInfo<Object> objectPageInfo = new PageInfo<>(objects);
        long total = objectPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("companies",companies);
        map.put("total",total);
        return map;
    }

    //插入公司数据
    public int insertCompany(Company company){
        return companyMapper.insert(company);
    }

    //根据id修改数据
    public int updateCompany(Company company){
        return companyMapper.updateByPrimaryKeySelective(company);
    }

    //根据id删除数据
    public int deleteCompany(Long id){
        return companyMapper.deleteByPrimaryKey(id);
    }
    //通过id查询数据
    public Company selectById(){
        return companyMapper.selectById();
    }
}
